d.screen_on()  # 打开屏幕
d.app_start("com.bluephone")
print(d.device_info)  # 获取详细的设备信息，selected和enable
d.app_current()  # 获取前台应用 packageName, activity
# d.press('recent')   #打开任务管理器
# d.swipe(0.764, 0.465,0.156, 0.474)  #左滑动关闭
# d.press('left')

d.app_start("com.bluephone")

d.app_current()  # 获取前台应用 packageName, activity
# d.press('recent')   #打开任务管理器
# d.swipe(0.764, 0.465,0.156, 0.474)  #左滑动关闭
# d.press('left')
d.app_start("com.bluephone")  # 启动应用
d(resourceId="com.bluephone:id/list_data").click()
print("关闭固定箱码，科室装袋-----------------------------------------------")
# 用户使用，删除本地垃圾,因为第一步操作的时候可能字典登录，所有try
try:
    d(resourceId="com.bluephone:id/systemSet3").click()
except:
    d.xpath(
        '//*[@resource-id="com.bluephone:id/rl_main"]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click()
finally:
    pass
try:
    d(resourceId="com.bluephone:id/data2").click()  # 本地删除按钮
except:
    d(resourceId="com.bluephone:id/systemSet3").click()
    d(resourceId="com.bluephone:id/data2").click()  # 本地删除按钮

d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()  # 确定按钮
d(resourceId="com.bluephone:id/iv_back").click()  # 返回首页

# 选择医院
d(resourceId="com.bluephone:id/systemSet1").click()  # 点击首次使用按钮
d.send_keys("展厅", clear=True)  # 搜索框输入“展厅”
d(resourceId="com.bluephone:id/tvName", text="展厅医院").click()  # 选择展厅医院
d(resourceId="com.bluephone:id/iv_back").click()  # 返回首页

# 系统设置，选择浙江模式
d(resourceId="com.bluephone:id/systemSet4").click()

d(resourceId="android:id/input").click()
d.send_keys("330624", clear=True)
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d(resourceId="com.bluephone:id/systemSet4").click()
d(resourceId="com.bluephone:id/spinner1").click()
d(resourceId="com.bluephone:id/tvName", text="浙江").click()
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d(resourceId="com.bluephone:id/systemSet4").click()
d.send_keys("330624", clear=True)
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d(resourceId="com.bluephone:id/systemSet4").click()
d(resourceId="com.bluephone:id/spinner2").click()  # 选择浙江模式

# 选择纯扫描版
d(resourceId="com.bluephone:id/tvName", text="纯扫描版").click()
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()

# 因为首次配置纯扫描版会自动登录
try:
    # 系统设置，装袋加固定箱码配置
    d(resourceId="com.bluephone:id/systemSet4").click()
    d.send_keys("330624", clear=True)
    d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
    d(resourceId="com.bluephone:id/systemSet4").click()
    time.sleep(1)
    d.swipe_ext("up")
    d(resourceId="com.bluephone:id/collectmode1").click()  # 点击科室装袋
    if (d(textContains="固定箱码(关闭)")):
        print('已经关闭固定箱码')
    else:
        print('固定箱码未关闭，自动关闭固定箱码中')
        d(resourceId="com.bluephone:id/fixBoxQRCode").click()
        print("关闭开固定箱码成功")
    d.keyevent('back')  # 单击返回按钮
except:
    d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
    d.xpath(
        '//*[@resource-id="com.bluephone:id/rl_main"]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click()
    d(resourceId="com.bluephone:id/systemSet4").click()
    d.send_keys("330624", clear=True)
    d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
    d(resourceId="com.bluephone:id/systemSet4").click()
    time.sleep(1)
    d.swipe_ext("up")
    d(resourceId="com.bluephone:id/collectmode1").click()  # 点击科室装袋
    if (d(textContains="固定箱码(关闭)").exists):
        print('已经关闭固定箱码')
    else:
        print('固定箱码未关闭，自动关闭固定箱码中')
        d(resourceId="com.bluephone:id/fixBoxQRCode").click()
        print("关闭固定箱码成功")
    d.keyevent('back')  # 单击返回按钮


def scan(x):
    os.system("adb shell am broadcast -a android.intent.ACTION_DECODE_DATA --es barcode_string %s" % x)
    time.sleep(1)


scan("9999010001A1030")

# 回收操作
# 回收操作-科室和护士输入代号认证
d(resourceId="com.bluephone:id/depart_name").click()
d.send_keys("001", clear=True)
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d(resourceId="com.bluephone:id/doctors_name").click()
d.send_keys("001", clear=True)
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
scan("9999010001B1048")  # 扫自动化科室
scan("9999010001A2080")  # 扫自动化护士
scan("https://zjrongjia.com/QRTrace/?21v300002646900010")  # 扫损伤性1
d(resourceId="com.bluephone:id/prompt_text").click()
d.send_keys("0.3", clear=True)
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d.xpath(
    '//*[@resource-id="com.bluephone:id/types_rv"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]').click()

# 入库操作
d(resourceId="com.bluephone:id/store_main").click()
scan("9999010001B2001")  # 扫暂存间二维码
scan("https://zjrongjia.com/QRTrace/?70v300002647400012	")  # 扫箱码1
scan("https://zjrongjia.com/QRTrace/?21v300002646900010")  # 扫损伤性1

# 出库操作
scan("9999010001A3013")
scan("9999010001A3013")
d(resourceId="com.bluephone:id/out_lib_all_primary").click()  # 点击医废全选
d(resourceId="com.bluephone:id/out_lib_all_deputy").click()  # 点击可回收全选
d(resourceId="com.bluephone:id/out_lib").click()  # 点击出库按钮
d(resourceId="com.bluephone:id/out_lib_history").click()  # 点击出库明细报表
time.sleep(0.5)
d.swipe_ext('up')
time.sleep(0.5)
d.press('back')

# 点击同步按钮
d(resourceId="com.bluephone:id/syncAndLogout").click()

# 返回首页，注销操作
d(resourceId="com.bluephone:id/recycle_main").click()
d.xpath(
    '//*[@resource-id="com.bluephone:id/rl_main"]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click()
# d.keyevent('back')  #单击返回按钮
# d.keyevent('back')  #单击返回按钮


# 打开固定箱码，科室装袋=====================================================================================================================
d.app_start("com.bluephone")
# 用户使用，删除本地垃圾,因为第一步操作的时候可能字典登录，所有try
print("打开固定箱码，科室装袋---------------------------------------------------------")
try:
    d(resourceId="com.bluephone:id/systemSet3").click()
except:
    d.xpath(
        '//*[@resource-id="com.bluephone:id/rl_main"]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click()
finally:
    pass

try:
    d(resourceId="com.bluephone:id/data2").click()
except:
    d(resourceId="com.bluephone:id/systemSet3").click()
    d(resourceId="com.bluephone:id/data2").click()

d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d(resourceId="com.bluephone:id/iv_back").click()

# #选择医院
d(resourceId="com.bluephone:id/systemSet1").click()  # 点击首次使用按钮
d.send_keys("展厅", clear=True)  # 搜索框输入“展厅”
d(resourceId="com.bluephone:id/tvName", text="展厅医院").click()  # 选择展厅医院
d(resourceId="com.bluephone:id/iv_back").click()  # 返回首页

# 系统设置，选择浙江模式
d(resourceId="com.bluephone:id/systemSet4").click()
d(resourceId="android:id/input").click()
d.send_keys("330624", clear=True)
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d(resourceId="com.bluephone:id/systemSet4").click()
d(resourceId="com.bluephone:id/spinner1").click()
d(resourceId="com.bluephone:id/tvName", text="浙江").click()
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()

# 选择纯扫描版
d(resourceId="com.bluephone:id/systemSet4").click()
d.send_keys("330624", clear=True)
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d(resourceId="com.bluephone:id/systemSet4").click()
d(resourceId="com.bluephone:id/spinner2").click()
d(resourceId="com.bluephone:id/tvName", text="纯扫描版").click()
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()

# 因为首次配置纯扫描版会自动登录
try:
    # 系统设置，装袋加固定箱码配置
    d(resourceId="com.bluephone:id/systemSet4").click()
    d.send_keys("330624", clear=True)
    d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
    d(resourceId="com.bluephone:id/systemSet4").click()
    time.sleep(1)
    d.swipe_ext("up")
    d(resourceId="com.bluephone:id/collectmode1").click()  # 点击科室装袋
    if (d(textContains="固定箱码(打开)").exists):
        print('已经打开固定箱码')
    else:
        print('固定箱码未打开，自动打开固定箱码中')
        d(resourceId="com.bluephone:id/fixBoxQRCode").click()
        print("打开固定箱码成功")

    d.keyevent('back')  # 单击返回按钮
except:
    d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
    d.xpath(
        '//*[@resource-id="com.bluephone:id/rl_main"]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click()
    d(resourceId="com.bluephone:id/systemSet4").click()
    d.send_keys("330624", clear=True)
    d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
    d(resourceId="com.bluephone:id/systemSet4").click()
    time.sleep(1)
    d.swipe_ext("up")
    d(resourceId="com.bluephone:id/collectmode1").click()  # 点击科室装袋
    if (d(textContains="固定箱码(打开)")):
        print('已经打开固定箱码')
    else:
        print('固定箱码未打开，自动打开固定箱码中')
        d(resourceId="com.bluephone:id/fixBoxQRCode").click()
        print("打开固定箱码成功")

    d.keyevent('back')  # 单击返回按钮

# 回收操作-科室和护士输入代号认证
scan("9999010001A1001")
d(resourceId="com.bluephone:id/depart_name").click()
d.send_keys("001", clear=True)  # 科室输入001认证
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
d(resourceId="com.bluephone:id/doctors_name").click()
d.send_keys("001", clear=True)  # 护士输入001认证
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()
scan("9999010001B1048")  # 扫自动化科室
scan("9999010001A2080")  # 扫自动化护士
scan("https://zjrongjia.com/QRTrace/?21v300002646900010")  # 扫损伤性1
d(resourceId="com.bluephone:id/prompt_text").click()  # 点击置零按钮
d.send_keys("0.5", clear=True)  # 输入重量0.5kg
d(resourceId="com.bluephone:id/md_buttonDefaultPositive").click()  # 点击确定按钮
d.xpath(
    '//*[@resource-id="com.bluephone:id/types_rv"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]').click()

# 入库操作
d(resourceId="com.bluephone:id/store_main").click()
scan("9999010001B2001")  # 扫暂存间二维码
scan("https://zjrongjia.com/QRTrace/?70v300002647400012	")  # 扫标签箱码1
# scan("2330600990024F05")    #这个是固定箱码
scan("https://zjrongjia.com/QRTrace/?21v300002646900010")  # 扫损伤性1

# 出库操作
scan("9999010001A3013")
d(resourceId="com.bluephone:id/out_lib_all_primary").click()  # 点击医废全选
d(resourceId="com.bluephone:id/out_lib_all_deputy").click()  # 点击可回收全选
d(resourceId="com.bluephone:id/out_lib").click()  # 点击出库按钮
d(resourceId="com.bluephone:id/out_lib_history").click()  # 点击出库明细报表
time.sleep(0.5)
d.swipe_ext('up')
time.sleep(0.5)
d.press('back')

# 点击同步按钮
d(resourceId="com.bluephone:id/syncAndLogout").click()

# 返回首页，注销操作
d(resourceId="com.bluephone:id/recycle_main").click()  # 点击回收按钮
d.xpath(
    '//*[@resource-id="com.bluephone:id/rl_main"]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click()  # 点击注销按钮

# d.xpath('//*[@resource-id="com.bluephone:id/list_data"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]').click()
# d(resourceId="com.bluephone:id/prompt_right_btn").click()
# d(resourceId="com.bluephone:id/item_del").click()
# d(resourceId="com.bluephone:id/prompt_right_btn").click()
# d(resourceId="com.bluephone:id/recycle_manual").click()
# d(resourceId="com.bluephone:id/recycle_auto").click()
# d.send_keys("6", clear=True)

# # 获取 toast, 当没有找到 toast 消息时，返回 default 内容
# d.toast.get_message(timout=5,default='no toast')
# # 清空 toast 缓存
# d.toast.reset()


d.xpath(
    '//*[@resource-id="com.bluephone:id/list_data"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]').click()
d.xpath('//*[@resource-id="com.bluephone:id/store_main"]').click()
d.xpath('//*[@resource-id="com.bluephone:id/store_main"]').click()
d.xpath('//*[@resource-id="com.bluephone:id/store_main"]').click()
d(resourceId="com.bluephone:id/store_main").click()
d(resourceId="com.bluephone:id/recycle_main").click()